package cayte.xiami.in.ui.setting;

import net.youmi.android.offers.PointsChangeNotify;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import cayte.xiami.in.R;
import cayte.xiami.in.ad.PointManager;
import cayte.xiami.in.ad.AdUtil;
import cayte.xiami.in.helper.Check;
import cayte.xiami.in.ui.TitleView;

import com.umeng.analytics.MobclickAgent;

public class GoldCenterActivity extends Activity implements PointsChangeNotify {

	private TextView hasPoints;
	private Button openYoumiOffer;
	private TitleView title;

	private PointManager pointManager;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gold_center);

		pointManager = PointManager.instance(this);

		title = new TitleView(this);
		title.setText(R.string.goldCenter);
		title.setLeftImage(R.drawable.back, new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				onBackPressed();
			}
		});
		hasPoints = (TextView) findViewById(R.id.goldHasPoints);
		openYoumiOffer = (Button) findViewById(R.id.youmiOffer);

		openYoumiOffer.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				pointManager.openYoumiOffer(GoldCenterActivity.this);
			}
		});

		//
		AdUtil.register(this, this);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		MobclickAgent.onResume(this);
		initData();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		MobclickAgent.onPause(this);
	}

	private void initData() {
		// TODO Auto-generated method stub
		hasPoints.setText(getString(R.string.autoHasPoints)
				+ pointManager.queryPoints());
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
		overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
	}

	@Override
	public void onPointBalanceChange(int points) {
		hasPoints.setText(getString(R.string.autoHasPoints) + points);
		MobclickAgent.onEvent(this, "points_record",
				points + "-" + Check.getAccount(this));
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		//
		AdUtil.unregister(this, this);
	}
}
